<?php

class CategoryController extends Controller {
    public function actionIndex() {
        Yii::app()->language = 'vi';
        if (!isset($_GET['cat_id'])) throw new CHttpException(999, 'Invalid request');
        $cat_id = $_GET['cat_id'];
        $location = isset($_GET['location']) ? $_GET['location'] : 0;
        $location_name = TreTodayCommon::getLocationName($location);
        $model = Product::model()->getForGrid($cat_id, $location);
        $category = Category::model()->findByPk($cat_id);

        #region SEO
        if (isset($_GET['Product_page'])) {
            Yii::app()->clientScript->registerMetaTag($category->description, 'description');
            Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_keywords'], 'keywords');

            if ($location_name == '') {
                $this->pageTitle = 'Rao vặt hot | ' . $category->cat_name . ' | Trang ' . $_GET['Product_page'];
            } else {
                $this->pageTitle = 'Rao vặt hot | ' . $location_name . ' | ' . $category->cat_name . ' | Trang ' . $_GET['Product_page'];
            }

        } else {
            Yii::app()->clientScript->registerMetaTag($category->description, 'description');
            Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_keywords'], 'keywords');

            if ($location_name == '') {
                $this->pageTitle = 'Rao vặt hot | ' . $category->cat_name;
            } else {
                $this->pageTitle = 'Rao vặt hot | ' . $location_name . ' | ' . $category->cat_name;
            }

        }
        Yii::app()->clientScript->registerLinkTag('canonical', null, TreTodayCommon::getCurrentUrl());

        #endregion SEO

        #region user interested
        $interested = UserInterested::model()->findByAttributes(array('user_ip' => TreTodayCommon::getUserIp(), 'category_id' => $cat_id));
        if ($interested == null) {
            $interested = new UserInterested();
            $interested->category_id = $cat_id;
            $interested->priority = 1;
            $interested->user_id = Yii::app()->user->id;
            $interested->user_ip = TreTodayCommon::getUserIp();
            $interested->save();
        } else {
            $interested->priority++;
            $interested->save();
        }
        #endregion

        $this->render('index', array('model' => $model, 'category' => $category));
    }

    public function beforeaction() {
        if (!isset(Yii::app()->request->cookies['rvhlayout'])) {
            $cookie = new  CHttpCookie('layout', '//layouts/main');
            $cookie->expire = time() + 60 * 60 * 24 * 10; //10 days
            Yii::app()->request->cookies['rvhlayout'] = $cookie;
        }
        $cookie = Yii::app()->request->cookies['rvhlayout'];
        $this->layout = $cookie->value;
        return true;
    }

    public function actionSearch() {
        Yii::app()->language = 'vi';
        if (!isset($_GET['cat_id'])) throw new CHttpException(999, 'Invalid request');
        if (!isset($_GET['keyword'])) throw new CHttpException(999, 'Invalid request');
        $cat_id = $_GET['cat_id'];
        $keyword = $_GET['keyword'];

        $category = Category::model()->findByPk($_GET['cat_id']);

        #region SEO
        Yii::app()->clientScript->registerLinkTag('canonical', null, TreTodayCommon::getCurrentUrl());
        if (isset($_GET['Product_page'])) {

            Yii::app()->clientScript->registerMetaTag(($category == null || $category->description == "") ? Yii::app()->params['meta_description'] : "$category->description", 'description');
            Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_keywords'] . ' ' . $keyword, 'keywords');
            $this->pageTitle = 'Rao vặt hot | ' . ($category == null ? ("Tìm kiếm | $keyword | Trang " . $_GET['Product_page']) : $category->cat_name . " | $keyword | Trang " . $_GET['Product_page']);
        } else {
            Yii::app()->clientScript->registerMetaTag(($category == null || $category->description == "") ? Yii::app()->params['meta_description'] : "$category->description", 'description');
            Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_keywords'] . ' ' . $keyword, 'keywords');
            $this->pageTitle = 'Rao vặt hot | ' . ($category == null ? "Tìm kiếm | $keyword" : $category->cat_name . " | $keyword");
        }

        #endregion SEO

        $model = Product::model()->searchForGrid($keyword, $cat_id);
        $this->render('search', array('model' => $model, 'keyword' => $keyword));
    }
}